package cn.edu.gcu.lost.sys.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gcu.lost.sys.dao.UserDao;
import cn.edu.gcu.lost.sys.entity.Menu;
import cn.edu.gcu.lost.sys.entity.Role;
import cn.edu.gcu.lost.sys.entity.User;
import cn.edu.gcu.lost.sys.service.UserService;
import cn.edu.gcu.lost.sys.util.UserUtils;

@Controller
@RequestMapping(value="user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping("findList")
	@RequiresPermissions("sys:user")
	public Object findList(@RequestParam(required= false ) String search,@RequestParam(defaultValue = "0")String start,@RequestParam(defaultValue = "10")String size) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("userList",userService.findList(search,Integer.parseInt(start), Integer.parseInt(size)));
		resultMap.put("result", "success");
		resultMap.put("data",dataMap);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("save")
	@RequiresPermissions("sys:user")
	public Object save(User user) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			if(userService.save(user)) {
				UserUtils.clearCache(user);
				resultMap.put("result", "success");
				resultMap.put("data", null);
			}else {
				resultMap.put("result", "faild");
				resultMap.put("cause", "save process error");
			}
		}catch(RuntimeSqlException e) {
			e.printStackTrace();
			resultMap.put("result", "faild");
			resultMap.put("cause", "save process error");
		}finally {
			return resultMap;
		}
	}
	
	@ResponseBody
	@RequestMapping("delete")
	@RequiresPermissions("sys:user")
	public Object delete(User user) {
		Map<String, Object> resultMap = new HashMap<>();
		user = UserUtils.get(user.getId());
		if(userService.delete(user)) {
			UserUtils.clearCache(user);
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "delete process error");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("updatePassword")
	public Object updatePwd(String password) {
		Map<String, Object> resultMap = new HashMap<>();
		User user = UserUtils.getUser();
		user.setPassword(password);
		if(userService.updatePassword(user)) {
			UserUtils.clearCache(user);
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "update process error");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("updateSelf")
	public Object updateSelf(User user) {
		Map<String,Object> resultMap = new HashMap<>();
		user.setId(UserUtils.getUser().getId());
		user.setNo(UserUtils.getUser().getNo());
		if(userService.updateSelf(user)) {
			UserUtils.clearCache(user);
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "update process error");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("isExistByNo")
	public Object isExistByNo(User user) {
		Map<String,Object> resultMap = new HashMap<>();
		if(userService.isExistByNo(user)) {
			resultMap.put("result", "success");
			resultMap.put("data", null);
		}else {
			resultMap.put("result", "faild");
			resultMap.put("cause", "no exist");
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("findSelf")
	public Object findSelf() {
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("user", UserUtils.getUser());
		resultMap.put("result", "success");
		resultMap.put("data", dataMap);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("countAllNum")
	@RequiresPermissions("sys:user")
	public Object countAllNum(@RequestParam(required= false ) String search) {
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("result", "success");
		resultMap.put("data", userService.countAllNum(search).toString());
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("bs/findDetail")
	@RequiresPermissions("sys:user")
	public Object findDetail(User user) {
		Map<String, Object> resultMap = new HashMap<>();
		if(StringUtils.isBlank(user.getId())) {
			resultMap.put("result", "faild");
			resultMap.put("cause", "param error");
		}else {
			resultMap.put("result", "success");
			resultMap.put("data",userService.findDetail(user));
		}
		return resultMap;
	}
}
